import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ThreadBlockTest{
    // 生产者消费者测试
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();

        //创建两个线程来当作消费者和生产者
        Thread customer = new Thread(()->{

            while(true){
                try {
                    Integer result = blockingQueue.take();
                    System.out.println("消费元素: "+result);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        customer.start();

        Thread producer = new Thread(()->{
            int count = 0;
            while(true){
                try {
                    blockingQueue.put(count);
                    System.out.println("生产元素: "+count);
                    count++;
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();

        // 会发现,运行之后 consumer 里面虽然没有sleep , 但是运行结果的 consumer 其实还是跟着 producer 走的
        // 那是因为 consumer 中 的take方法产生了阻塞

    }
}
